<?php
/**
 * Zerlegt eine Url in ihre Bestandteile
 * 
 * Format:
 * scheme://user:pass@domain/path?queryString#fragmentId
 * http://user:password@www.loresoft.de:port/path1/path2?queryString+FragmentId
 * einzelne Bestandteile der Url koennen weg gelassen werden
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2013 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 *
 * */


Class Yapary_WWW_Url_UrlData
{
/**
   $regex = "((https?|ftp)\:\/\/)?"; // SCHEME
    $regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass
    $regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP
    $regex .= "(\:[0-9]{2,5})?"; // Port
    $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path
    $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query
    $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor 
 * */
	private $arrPicies = array();

	
	/**
	 * @param array $arrPicies	: Format
	 * [0] => http://www.loresoft.de/var1/var2?var3=3&var4=4#Sprungmarke
	 * [1] => http://www.loresoft.de
	 * [2] => user:pass@
	 * [3] => :port
	 * [4] => /var1/var2
	 * [5] => ?var3=3&var4=4
	 * [6] => #Sprungmarke
	 * */
	function __construct (array $_arrPicies) {
		$this->arrPicies = $_arrPicies;
	}

	
	/**
	 * @desc Ausgabe des Scheme der URL (http|https|ftp|...)
	 * @return string
	 * */
	public function getScheme () {
		$arrResult = array();
		
		preg_match('/^(\w{1,6})\:\/\//i', $this->arrPicies[1], $arrResult);
		
		if (!isset($arrResult[1])) {
			return '';
		}
		
		return $arrResult[1];
	}
	

	/**
	 * @desc Ausgabe des Domainnames der URL (domain.de)
	 * @return string
	 * */
	public function getDomain () {
		$arrResult = array();
		
		preg_match('/^\w{1,6}\:\/\/(\w*:\w*@|\w*@)?([a-z0-9-.]*)?/i', $this->arrPicies[1], $arrResult);

		if (!isset($arrResult[2])) {
			return '';
		}
		
		return $arrResult[2];
		
	}
	
	
	/**
	 * @desc Ausgabe des Usernamens der URL (user:pass@)
	 * @return string
	 * */
	public function getUsername () {
		$arrResult = array();
		
		if (!isset($this->arrPicies[2])) {
			return '';
		}
		
		$arrResult = explode(':', $this->arrPicies[2]);
		if (!isset($arrResult[0])) {
			return '';
		}
		if (strpos($arrResult[0], '@')) {
			str_replace('@', '', $arrResult[0]);
		}
		
		return $arrResult[0];
	}
	
	
	/**
	 * @desc Ausgabe des Passworts der URL (user:pass@)
	 * @return string
	 * */
	public function getPassword () {
		$arrResult = array();
		
		if (!isset($this->arrPicies[2])) {
			return '';
		}
		
		$arrResult = explode(':', $this->arrPicies[2]);
		if (!isset($arrResult[1])) {
			return '';
		}
		$arrResult = explode('@', $arrResult[1]);
		if (!isset($arrResult[0])) {
			return '';
		}
		
		return $arrResult[0];
	}
	
	
	/**
	 * @desc Ausgabe des Ports der URL (:Port)
	 * @return intger|NULL
	 * */
	public function getPort () {
		$arrResult = array();
		
		if (!isset($this->arrPicies[3])) {
			return NULL;
		}
		
		$arrResult = explode(':', $this->arrPicies[3]);
		if (!isset($arrResult[1])) {
			return NULL;
		}
		
		return (int) $arrResult[1];
	}

	
	/**
	 * @desc Ausgabe des Path der URL (/path1/path2...)
	 * @return Yapary_WWW_Url_UrlPathData
	 * */
	public function getPath () {
		if (!isset($this->arrPicies[4])) {
			return new Yapary_WWW_Url_UrlPathData('');
		}

		return new Yapary_WWW_Url_UrlPathData($this->arrPicies[4]);
	}

	
	/**
	 * @desc Ausgabe des QueryStrings der URL (?var1=1&var2=2...)
	 * @return Yapary_WWW_Url_UrlQueryStringData
	 * */
	public function getQueryString () {
		if (!isset($this->arrPicies[5])) {
			return new Yapary_WWW_Url_UrlQueryStringData('');
		}
		
		return new Yapary_WWW_Url_UrlQueryStringData($this->arrPicies[5]);
	}

	
	/**
	 * @desc Ausgabe des Fragments der URL (#var)
	 * @return string
	 * */
	public function getFragmentId () {
		$arrResult = array();
		
		if (!isset($this->arrPicies[7])) {
			return '';
		}
		
		$arrResult = explode('#', $this->arrPicies[7]);
		if (!isset($arrResult[1])) {
			return '';
		}

		return $arrResult[1];
	}
	
	
}



?>